#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import lxml.html

import re

import base64


def loop_for_parse():
    result = []
    i = 1
    while 1:
        url = 'http://www.cool-proxy.net/proxies/http_proxy_list/page:'\
            + str(i) + '/country_code:/port:/anonymous:1'
        i += 1
        print "[*] Parsing %s now" % url
        try:
            r = parse_proxy_list(url)
        except:
            continue
        if not r:
            break
        result += r
    return result


def parse_proxy_list(url):
    r = requests.get(url, timeout=5)
    if not r.ok:
        return False
    # ----------------------------------------
    # base 64
    #----------------------------------------
    proxy_list = []
    root = lxml.html.fromstring(r.content)
    # Parsing ports
    ports = root.xpath('//tr/td[2]/text()')
    # Parsing ip
    ips_base64 = re.findall('decode\("([^"]+)"', r.content)
    ips = [base64.decodestring(ip_base64) for ip_base64 in ips_base64]
    for ip, port in zip(ips, ports):
        proxy_list.append(ip + ':' + port)
    return proxy_list

if __name__ == '__main__':
    proxy_list = loop_for_parse()
    for i in range(len(proxy_list)):
        # print proxy_list[i]
        proxy_list[i] += '\n'
    with open('proxy_list/http_proxy_cool.txt', 'wb') as f:
        f.writelines(proxy_list)
